/**************************************************************************\
    Copyright Microsoft Corporation. All Rights Reserved.
\**************************************************************************/

namespace Conmigo.Widgets
{
    using System;
    using System.ComponentModel;
    using Microsoft.Communications.Contacts;
    using Standard;

    public class ContactInfo : ContactView, INotifyPropertyChanged
    {
        private string _notes;
        private string _workWebsite;
        private string _personalWebsite;
        private string _webFeed;

        private void _OnPropertyChanged(string propertyName)
        {
            Assert.IsFalse(string.IsNullOrEmpty(propertyName));

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public PhoneNumberBuilder HomePhone { get; private set; }
        public PhoneNumberBuilder HomeFax { get; private set; }
        public PhoneNumberBuilder WorkPhone { get; private set; }
        public PhoneNumberBuilder WorkFax { get; private set; }
        public PhoneNumberBuilder Cellular { get; private set; }
        public PhoneNumberBuilder Pager { get; private set; }
        public NameBuilder Name { get; private set; }
        public PositionBuilder Job { get; private set; }
        public PhysicalAddressBuilder WorkAddress { get; private set; }
        public PhysicalAddressBuilder HomeAddress { get; private set; }
        public EmailAddressBuilder Email { get; private set; }
        public PhotoBuilder UserTile { get; private set; }

        public string WorkWebsite
        {
            get { return _workWebsite; }
            set
            {
                if (_workWebsite != value)
                {
                    _workWebsite = value;
                    _OnPropertyChanged("WorkWebsite");
                    Uri workUri;
                    if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out workUri))
                    {
                        WorkWebsiteUri = workUri;
                    }
                    else
                    {
                        WorkWebsiteUri = new Uri("about:blank");
                    }
                    _OnPropertyChanged("WorkWebsiteUri");
                }
            }
        }

        public Uri WorkWebsiteUri { get; private set; }

        public string PersonalWebsite
        {
            get { return _personalWebsite; }
            set
            {
                if (_personalWebsite != value)
                {
                    _personalWebsite = value;
                    _OnPropertyChanged("PersonalWebsite");
                    Uri homeUri;
                    if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out homeUri))
                    {
                        PersonalWebsiteUri = homeUri;
                    }
                    else
                    {
                        PersonalWebsiteUri = new Uri("about:blank");
                    }
                    _OnPropertyChanged("PersonalWebsiteUri");
                }
            }
        }

        public Uri PersonalWebsiteUri { get; private set; }

        public string WebFeed
        {
            get { return _webFeed; }
            set
            {
                if (_webFeed != value)
                {
                    _webFeed = value;
                    _OnPropertyChanged("WebFeed");
                    Uri feedUri;
                    if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out feedUri))
                    {
                        WorkWebsiteUri = feedUri;
                    }
                    else
                    {
                        WebFeedUri = new Uri("about:blank");
                    }
                    _OnPropertyChanged("WebFeedUri");
                }
            }
        }

        public Uri WebFeedUri { get; private set; }

        public string Notes
        {
            get { return _notes; }
            set
            {
                if (_notes != value)
                {
                    _notes = value;
                    _OnPropertyChanged("Notes");
                }
            }
        }

        public ContactInfo(Contact contact)
            : base(contact)
        {
            Name = new NameBuilder(Source.Names.Default);
            HomeAddress = new PhysicalAddressBuilder(Source.Addresses[PropertyLabels.Personal]);
            WorkAddress = new PhysicalAddressBuilder(Source.Addresses[PropertyLabels.Business]);
            Job = new PositionBuilder(Source.Positions[PropertyLabels.Business]);
            HomePhone = new PhoneNumberBuilder(Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice]);
            WorkPhone = new PhoneNumberBuilder(Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice]);
            HomeFax = new PhoneNumberBuilder(Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Fax]);
            WorkFax = new PhoneNumberBuilder(Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Fax]);
            Pager = new PhoneNumberBuilder(Source.PhoneNumbers[PhoneLabels.Pager]);
            Cellular = new PhoneNumberBuilder(Source.PhoneNumbers[PhoneLabels.Cellular]);
            Email = new EmailAddressBuilder(Source.EmailAddresses.Default);
            _notes = Source.Notes;
            UserTile = new PhotoBuilder(Source.Photos[PhotoLabels.UserTile]);
            UserTile.PropertyChanged += _OnUserTileChanged;

            Uri uri = Source.Urls[PropertyLabels.Business];
            if (null != uri)
            {
                _workWebsite = uri.ToString();
            }
            uri = Source.Urls[PropertyLabels.Personal];
            if (null != uri)
            {
                _personalWebsite = uri.ToString();
            }
            uri = Source.Urls[UrlLabels.Rss];
            if (null != uri)
            {
                _webFeed = uri.ToString();
            }
        }

        private void _OnUserTileChanged(object sender, PropertyChangedEventArgs e)
        {
            // If any property in the user tile changes then notify changes by the UserTile name.
            _OnPropertyChanged("UserTile");
        }

        public void SaveToSource()
        {
            if (string.IsNullOrEmpty(Name.FormattedName))
            {
                Name.FormattedName = Microsoft.Communications.Contacts.Name.FormatName(Name.GivenName, Name.MiddleName, Name.FamilyName, NameCatenationOrder.GivenMiddleFamily);
            }

            Source.Names.Default = Name;
            Source.Addresses[PropertyLabels.Business] = WorkAddress;
            Source.Addresses[PropertyLabels.Personal] = HomeAddress;
            Source.Positions[PropertyLabels.Business] = Job;
            Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice] = HomePhone;
            Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Voice] = WorkPhone;
            Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Fax] = HomeFax;
            Source.PhoneNumbers[PropertyLabels.Business, PhoneLabels.Fax] = WorkFax;
            Source.PhoneNumbers[PropertyLabels.Personal, PhoneLabels.Voice] = HomePhone;
            Source.PhoneNumbers[PhoneLabels.Pager] = Pager;
            Source.PhoneNumbers[PhoneLabels.Cellular] = Cellular;
            Source.EmailAddresses.Default = Email;
            Source.Notes = Notes;
            Source.Photos[PhotoLabels.UserTile] = UserTile;
            
            Uri uri;
            Uri.TryCreate(WorkWebsite, UriKind.RelativeOrAbsolute, out uri);
            Source.Urls[PropertyLabels.Business] = uri;
            Uri.TryCreate(PersonalWebsite, UriKind.RelativeOrAbsolute, out uri);
            Source.Urls[PropertyLabels.Personal] = uri;
            Uri.TryCreate(WebFeed, UriKind.RelativeOrAbsolute, out uri);
            Source.Urls[UrlLabels.Rss] = uri;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
